<template>
  <div class="cs-incident-table">
    <el-table
      v-if="this.tableData.length > 0"
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        v-for="item in tableHead"
        :key="item.key"
        :prop="item.key"
        :label="item.label">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script type="text/ecmascript-6">
  export default {
    name: 'cs-incident-table',
    data () {
      return {
        tableHead: [],
        tableData: []
      }
    },
    methods: {
      // 获取表格数据
      getTableData (arr) {
        this.tableHead = []
        this.tableData = []
        if (arr.length === 0) {
          return
        }
        this.tableData = arr
        if ('MC000154__GSD' in arr[0]) {
          this.tableHead.push({
            key: 'MC000154__GSD',
            label: '日期'
          })
        }
        let gfList = []
        for (let key in arr[0]) {
          if (key.indexOf('__GF__') !== -1) {
            gfList.push(key)
          }
          if (key.indexOf('__GB') !== -1) {
            let str = []
            key.split('__').forEach(val => {
              for (let i = 0; i < this.data.B010019.length; i++) {
                if (val === this.data.B010019[i].code_value) {
                  str.push(this.data.B010019[i].code_value_nm_cns)
                }
              }
            })
            this.tableHead.push({
              key: key,
              label: str.join(',')
            })
          }
        }
        let list = []
        gfList.forEach(val => {
          let valArr = val.split('__')
          let strList = []
          for (let i = 0; i < this.data.B010019.length; i++) {
            if (this.data.B010019[i].code_value === valArr[0]) {
              strList.push(this.data.B010019[i].code_value_nm_cns)
              break
            }
          }
          for (let i = 0; i < this.data.META_TGT.length; i++) {
            if (this.data.META_TGT[i].meta_id === valArr[1]) {
              strList.push(this.data.META_TGT[i].tgt_name)
              break
            }
          }
          list[valArr[valArr.length - 1] - 1] = {
            key: val,
            label: strList.join(',')
          }
        })
        this.tableHead.push(...list)
      },
      handleClick (row) {
        let obj = {}
        if ('MC000154__GSD' in row) {
          obj.MC000154__GSD = row.MC000154__GSD
        }
        for (let key in row) {
          if (key.indexOf('__GB') !== -1) {
            obj[key] = row[key]
          }
        }
        this.$emit('on-goUserList', obj)
      }
    },
    created () {
      // 从sessionStorage中获取常量数据
      this.data = JSON.parse(window.sessionStorage.getItem('commonData'))
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">

</style>
